import React, { useEffect } from 'react';

function log(...args) {
  console.log(args.join(', '));
}

function* foo() {
  log('generator foo');

  try {
    const res = yield 'foo';
    log(res);
  } catch (e) {
    console.log(e);
  }
}

function scope() {
  const generator = foo();

  const res1 = generator.next();
  log(JSON.stringify(res1));

  // 1. 传参入 foo
  // const res2 = generator.next('scope');
  // log(JSON.stringify(res2));

  // 2. 抛出异常给 foo
  // const res2 = generator.throw(new Error('generator error'));
  // log(JSON.stringify(res2));

  // 3. 返回值
  const res2 = generator.return('return');
  log(JSON.stringify(res2));
}

export default function GeneratorDemo() {
  useEffect(scope, []);

  return (
    <div>
      <p>Generator 异步方案展示</p>
    </div>
  );
}
